import { Observable, Subject } from "rxjs";
// Aigle also could as Promise polyfill

// Create an observable that emits values every second
const observable = new Observable<number>((subscriber) => {
  let count = 0;
  const intervalId = window.setInterval(() => {
    subscriber.next(count++); // Emit the current count
    if (count > 5) {
      subscriber.complete(); // Complete the observable after 5 emissions
    }
  }, 1000);

  // Cleanup logic when the subscription is unsubscribed
  return () => {
    window.clearInterval(intervalId);
    console.log("Observable cleaned up");
  };
});

// reducer
// Subscribe to the observable
const subscription = observable.subscribe({
  next: (value) => console.log(`Received value: ${value}`),
  error: (err) => console.error(`Error: ${err}`),
  complete: () => console.log("Observable completed"),
});

// Unsubscribe after 3 seconds
window.setTimeout(() => {
  subscription.unsubscribe();
  console.log("Unsubscribed from observable");
}, 3000);

// Object.defineProperty(Aigle, "try", {
//   value: (fn: Function, ...fn_arg: unknown[]) => {
//     return new Promise((resolve, reject) => {
//       try {
//         const result = fn.apply(null, fn_arg);
//         resolve(result);
//       } catch (error) {
//         reject(error);
//       }
//     });
//   },
// });
